package com.greenova.theprojector.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.greenova.theprojector.biz.entities.Employee;
import com.greenova.theprojector.biz.services.EmployeeService;
import com.greenova.theprojector.biz.services.ProjectService;

/**
 * Handles requests for the application home page.
 */
@Controller
public class ProjectAssignmentController {
	
	private static final Logger logger = LoggerFactory.getLogger(ProjectAssignmentController.class);
	
	@Autowired
	private ProjectService projectService;
	
	@Autowired
	private EmployeeService employeeService;
	
	@ModelAttribute("allEmployees")
    public List<Employee> populateEmployees() {
        return employeeService.findAll();
    }
	
	@ModelAttribute("allUnassignedEmployees")
    public List<Employee> populateUnassignedEmployees() {
        return employeeService.findAllUnassigned();
    }
	
	@RequestMapping({"/","/theprojector"})
    public String showprojector(final Employee employee) {
        return "theprojector";
    }
	
	@RequestMapping(value="/theprojector", params={"addEmployee"})
    public String addEmployee(final Employee employee, final BindingResult bindingResult) {		
		employeeService.assign(employee);
        //model.clear();
        return "redirect:theprojector";
    }
	
	@RequestMapping(value="/theprojector", params={"removeEmployee"})
    public String removeEmployee(final Employee employee, final BindingResult bindingResult,final HttpServletRequest req) {
		final Integer employeeId = Integer.valueOf(req.getParameter("removeEmployee"));
		employee.setEmployeeId(employeeId);
		employeeService.unassign(employee);
        //model.clear();
        return "redirect:theprojector";
    }
}
